package com.company.core.aspect;

import com.company.core.result.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * @program: planet
 * @description: 登录验证
 * @author: jyz
 * @create: 2019-04-10 11:04
 **/
@Aspect
@Component
@Order(1)
public class LoginAspect {
    private static final Logger logger = LoggerFactory.getLogger(LoginAspect.class);
    @Pointcut("@annotation(com.company.core.annotation.Login)")
    public void serviceAspect(){}

    @Around("serviceAspect()")
    public Object beforeExec(ProceedingJoinPoint joinPoint) throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        String phone = (String)session.getAttribute("phone");
        logger.info("session信息 ------ > sessionId == "+session.getId()+"  当前用户userId == "+phone+"  有效期 == "+session.getMaxInactiveInterval());
        if (StringUtils.isEmpty(phone)){
            return ResultUtil.error(2,"未登录");
        }
        return joinPoint.proceed();
    }
}
